package com.wty.mianshi.circulation;

import com.wty.mianshi.common.Node;
import com.wty.mianshi.recursion.LinkedListCreator;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class LinkedCreator {

    /**
     * 循环创建链表
     * head是不动的，动的一直是prev，最后面的节点是prev
     * @param dataList
     * @return
     */
    public Node createLinkedList(List dataList) {

        Node prev = null;
        Node head = null;

        for (int i = 0; i < dataList.size(); i++) {
            Node node = new Node(dataList.get(i));
            if(prev!=null){
                prev.setNext(node);
            }else {
                head=node;
            }
            prev=node;


        }
        return head;
    }


    /**
     * 从链表的某一个节点开始打印链表各个节点存储的数据
     *
     * @param node
     */
    public void printLinkedList(Node node) {
        while (node != null) {
            System.out.print(node.getData());
            System.out.print(" ");
            node = node.getNext();
        }
        System.out.println();
    }


    public static void main(String args[]) {
        LinkedCreator linkedCreator=new LinkedCreator();
        Node node1 = linkedCreator.createLinkedList(new ArrayList());
        linkedCreator.printLinkedList(node1);

        Node node2 = linkedCreator.createLinkedList(Arrays.asList(1));
        linkedCreator.printLinkedList(node2);

        Node node3 = linkedCreator.createLinkedList(Arrays.asList(1, 2, 3, 4, 5));
        linkedCreator.printLinkedList(node3);
    }
}
